package com.xyzm.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.concurrent.TimeUnit;

public class MyScheduler {
    public static void main(String[] args) throws Exception{
        test1();
    }
    //SimpleTrigger可以实现在一个指定时间段内执行一次作业任务或一个时间段内多次执行作业任务。
    public static void test2() throws SchedulerException, InterruptedException {
        //创建调度器
        StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = stdSchedulerFactory.getScheduler();
        //创建JobDetail实例并与PrintWordJob绑定
        JobDetail jobDetail = JobBuilder.newJob(PrintWordJob.class).withIdentity("Job1", "Group1").build();
        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("Trigger1", "Trigger1")
                .startNow()//立马执行
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(1)//每秒钟执行一次
                        .repeatForever())//一直执行
                .build();
        scheduler.scheduleJob(jobDetail,trigger);
        System.out.println("--------scheduler start ! ------------");

        scheduler.start();
        TimeUnit.MINUTES.sleep(1);
        scheduler.shutdown();
        System.out.println("--------scheduler shutdown ! ------------");
    }
    public static void test1() throws SchedulerException, InterruptedException {
        //创建调度器
       /* StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = stdSchedulerFactory.getScheduler();
        //创建JobDetail实例并与PrintWordJob绑定
        JobDetail jobDetail = JobBuilder.newJob(PrintWordJob.class).withIdentity("Job1", "Group1").build();
        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("Trigger1", "Trigger1")
                .startNow()//立马执行
                .withSchedule()//一直执行
                .build();
        scheduler.scheduleJob(jobDetail,trigger);
        System.out.println("--------scheduler start ! ------------");

        scheduler.start();
        TimeUnit.MINUTES.sleep(1);
        scheduler.shutdown();
        System.out.println("--------scheduler shutdown ! ------------");*/

    }
}
